package dao.impl;

import dao.UserDao;
import domian.User;
import utils.BaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    //用户注册
    @Override
    public int addUser(User user) {
        String sql = "insert into user(tel,password,email,total) values(?,?,?,?)";

        Object[] obj = {user.getTel(),user.getPassword(),user.getEmail(),user.getTotal()};

        int i = BaseDAO.executeUpdate(sql, obj);
        return i;
    }

    //用户修改信息
    @Override
    public int updateUser(User user) {

        String sql = "update user set tel=?,password=?,realname=?,sex=?,headerimg=?,nickname=?,brithday=?,email=?,total=?,followsum=?,fansum=? where uid=?";

        Object[] obj = {user.getTel(),user.getPassword(),user.getRealname(),user.getSex(),
                user.getHeaderimg(),user.getNickname(),user.getBirthday(),user.getEmail(),
                user.getTotal(),user.getFollowsum(),user.getFansum(),user.getUid()};

        int i = BaseDAO.executeUpdate(sql, obj);
        return i;
    }

    @Override
    public User findUserById(Integer uid) {
        User user = null;
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //获取链接
            connection = BaseDAO.getConnection();
            String sql = "select * from user where uid=?";
            ps = connection.prepareStatement(sql);
            ps.setInt(1,uid);
            rs = ps.executeQuery();

            while (rs.next()){
                user = new User();
                user.setUid(rs.getInt("uid"));
                user.setTel(rs.getString("tel"));
                user.setPassword(rs.getString("password"));
                user.setRealname(rs.getString("realname"));
                user.setSex(rs.getString("sex"));
                user.setHeaderimg(rs.getString("headerimg"));
                user.setNickname(rs.getString("nickname"));
                user.setBirthday(rs.getString("brithday"));
                user.setEmail(rs.getString("email"));
                user.setTotal(rs.getInt("total"));
                user.setFollowsum(rs.getInt("followsum"));
                user.setFansum(rs.getInt("fansum"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,ps);
        }
        return user;
    }

    //用户登录
    @Override
    public User login(String tel) {
        User user = null;
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //获取链接
            connection = BaseDAO.getConnection();
            String sql = "select * from user where tel=?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,tel);
            rs = ps.executeQuery();

            while (rs.next()){
                user = new User();
                user.setUid(rs.getInt("uid"));
                user.setTel(rs.getString("tel"));
                user.setPassword(rs.getString("password"));
                user.setRealname(rs.getString("realname"));
                user.setSex(rs.getString("sex"));
                user.setHeaderimg(rs.getString("headerimg"));
                user.setNickname(rs.getString("nickname"));
                user.setBirthday(rs.getString("brithday"));
                user.setEmail(rs.getString("email"));
                user.setTotal(rs.getInt("total"));
                user.setFollowsum(rs.getInt("followsum"));
                user.setFansum(rs.getInt("fansum"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,ps);
        }
        return user;
    }
}
